function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _slicedToArray(arr, i) {
  return (
    _arrayWithHoles(arr) ||
    _iterableToArrayLimit(arr, i) ||
    _unsupportedIterableToArray(arr, i) ||
    _nonIterableRest()
  );
}
function _nonIterableRest() {
  throw new TypeError(
    'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _iterableToArrayLimit(arr, i) {
  var _i =
    null == arr
      ? null
      : ('undefined' != typeof Symbol && arr[Symbol.iterator]) || arr['@@iterator'];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else
        for (
          ;
          !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i);
          _n = !0
        );
    } catch (err) {
      (_d = !0), (_e = err);
    } finally {
      try {
        if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
/*
 * @Descripttion:
 * @Author: gll
 * @Date: 2023-11-21 11:31:07
 * @LastEditors: gll
 * @LastEditTime: 2023-11-23 17:08:13
 */
import { CaretDownOutlined, CaretUpOutlined } from '@ant-design/icons';
import { Tag as AntdTag } from 'antd';
import 'antd/es/tag/style/index.js';
import classnames from 'classnames';
import React, { forwardRef, useCallback, useMemo, useState } from 'react';
import createField from '../createField';
import './index.less';
var CheckableTag = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    options = _ref.options,
    style = _ref.style,
    _ref$multiple = _ref.multiple,
    multiple = _ref$multiple === void 0 ? true : _ref$multiple,
    folded = _ref.folded,
    tagProps = _ref.tagProps,
    checkedTagProps = _ref.checkedTagProps,
    className = _ref.className;
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    foldState = _useState2[0],
    setFoldState = _useState2[1];
  var checkedVals = useMemo(
    function () {
      if (multiple) return value !== null && value !== void 0 ? value : [];
      return value ? [value] : [];
    },
    [value, multiple],
  );
  var handleAdd = useCallback(
    function (key) {
      var newValue = multiple
        ? [].concat(
            _toConsumableArray(checkedVals !== null && checkedVals !== void 0 ? checkedVals : []),
            [key],
          )
        : key;
      onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
    },
    [checkedVals, multiple],
  );
  var handleRemove = useCallback(
    function (key) {
      if (multiple) {
        onChange === null || onChange === void 0
          ? void 0
          : onChange(
              (checkedVals !== null && checkedVals !== void 0 ? checkedVals : []).filter(function (
                it,
              ) {
                return it !== key;
              }),
            );
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(undefined);
      }
    },
    [checkedVals, multiple],
  );
  var getTagChild = function getTagChild(option) {
    return checkedVals !== null && checkedVals !== void 0 && checkedVals.includes(option.value)
      ? /*#__PURE__*/ React.createElement(
          AntdTag,
          _extends(
            {
              key: option.value,
              color: 'blue',
            },
            tagProps,
            checkedTagProps,
            {
              className: 'szzj-checkable-tag',
              onClick: function onClick() {
                return handleRemove(option.value);
              },
            },
          ),
          option.label,
        )
      : /*#__PURE__*/ React.createElement(
          AntdTag,
          _extends(
            {
              key: option.value,
            },
            tagProps,
            {
              className: 'szzj-checkable-tag',
              onClick: function onClick() {
                return handleAdd(option.value);
              },
            },
          ),
          option.label,
        );
  };
  return /*#__PURE__*/ React.createElement(
    'div',
    {
      className: classnames('szzj-checkable-tags', className),
      style: style,
      ref: ref,
    },
    /*#__PURE__*/ React.createElement(
      'div',
      {
        className: 'szzj-checkable-tag-wrap',
      },
      options.map(function (optionItem, index) {
        if (foldState && folded && index >= folded) return null;
        return getTagChild(optionItem);
      }),
    ),
    folded &&
      folded > 0 &&
      (options === null || options === void 0 ? void 0 : options.length) > folded &&
      /*#__PURE__*/ React.createElement(
        'a',
        {
          className: 'szzj-checkable-tags-expand',
          onClick: function onClick() {
            return setFoldState(!foldState);
          },
        },
        foldState ? '展开' : '收起',
        foldState
          ? /*#__PURE__*/ React.createElement(CaretDownOutlined, null)
          : /*#__PURE__*/ React.createElement(CaretUpOutlined, null),
      ),
  );
});
export default createField(CheckableTag);
